<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class DrugService extends BaseService
{
    //药品

    //选项
    public function getOption()
    {
        $arys = ['cate' => []];
        $cateOnes = $this->dbConn($this->dbDrugCate)
            ->where('del', 0)
            ->get();
        if (count($cateOnes)) {
            foreach ($cateOnes as $v) {
                $v = (array) $v;
                $arys['cate'][] = [
                    'id' => $v['id'],
                    'title' => $v['title'],
                ];
            }
        }
        return $arys;
    }

    /***
     * 列表
     * @param array $pages【start、limit】
     */
    public function getIndex($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbDrug)->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback();
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $cateOne = (array) $this->dbConn($this->dbDrugCate)
                ->where('id', $v['cate_id'])
                ->first();
            $cate_name = !empty($cateOne)&&$cateOne['title'] ? $cateOne['title'] : '-';
            $img = file_exists($v['img']) ? '/'.$v['img'] : '';
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'cate_name' => $cate_name,
                'img' => $img,
                'price' => toNumber($v['price']),
                'num' => $v['num'],
                'zhuzhi' => $v['zhuzhi'],
                'period' => $v['period'],
                'is_shelf' => $v['is_shelf'],
                'shelf_name' => $v['is_shelf'] ? '已上架'  : '已下架',
                'uptime' => $v['uptime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 详情
     */
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbDrug)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $cateOne = (array) $this->dbConn($this->dbDrugCate)
            ->where('id', $res['cate_id'])
            ->first();
        $cate_name = !empty($cateOne)&&$cateOne['title'] ? $cateOne['title'] : '-';
        $img = file_exists($res['img']) ? $res['img'] : '';
//        $symptom = $res['symptom'] ? explode('||', $res['symptom']) : [];
        $tagOnes = $this->dbConn($this->dbDrugTag)
            ->where('del', 0)
            ->where('drug_id', $res['id'])
            ->get();
        $tagList = [];
        if (count($tagOnes)) {
            foreach ($tagOnes as $tv) {
                $tagList[] = $tv->title;
            }
        }
        $arys = [
            'id' => $res['id'],
            'title' => $res['title'],
            'cate_id' => $res['cate_id'],
            'cate_name' => $cate_name,
            'img' => $img,
//            'symptom' => $symptom,
            'symptom' => $tagList,
            'price' => toNumber($res['price']),
            'num' => $res['num'],
            'zhuzhi' => $res['zhuzhi'],
            'yongfa' => $res['yongfa'],
            'period' => $res['period'],
            'instruction' => $res['instruction'],
        ];
        return $this->toback(0, '', $arys);
    }

    /**
     * 添加
     * @param array $pas
     */
    public function add($pas, $tagStr)
    {
        $pas['uptime'] = $pas['addtime'] = toDate();
        $lastid = $this->dbConn($this->dbDrug)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, 'error');
        }
        $this->tagModify($lastid, $tagStr);
        return $this->toback();
//        DB::beginTransaction(); //开启事务
//        try {
//            //更新订单状态
//            DB::commit(); //提交事务
//            return $this->toback();
//        } catch (\Exception $e) {
//            DB::rollBack(); //事务回滚
//            echo $e->getMessage();
//            return $this->toback(-1, 'error');
//        }
    }

    //刷新标签
    public function tagModify($drug_id, $str)
    {
        if (!$drug_id || !$str) return false;
        $tags = explode('||', $str);
        $titles = [];
        if ($tags) {
            foreach ($tags as $v) {
                $tagOne = (array) $this->dbConn($this->dbDrugTag)
                    ->where('del', 0)
                    ->where('drug_id', $drug_id)
                    ->where('title', $v)
                    ->first();
                if (!$tagOne) {
                    $this->dbConn($this->dbDrugTag)->insertGetId([
                        'drug_id' => $drug_id, 'title' => $v, 'addtime' => toDate(),
                    ]);
                    $titles[] = $v;
                } else {
                    $titles[] = $tagOne['title'];
                }
            }
            $this->dbConn($this->dbDrugTag)
                ->where('del', 0)
                ->where('drug_id', $drug_id)
                ->whereNotIn('title', $tags)
                ->update(['del' => 1, 'deltime' => toDate()]);
        }
        return true;
    }

    /**
     * 修改
     * @param int $id
     * @param array $pas
     */
    public function modify($id, $pas, $tagStr)
    {
        $res = (array) $this->dbConn($this->dbDrug)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有药品');
        }
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbDrug)->where('id', $id)->update($pas);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        $this->tagModify($id, $tagStr);
        return $this->toback();
    }

    /**
     * 上下架
     * @param int $id
     */
    public function shelfModify($id, $shelf)
    {
        $res = (array) $this->dbConn($this->dbDrug)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有药品');
        }
        $this->dbConn($this->dbDrug)->where('id', $id)->update(['is_shelf' => $shelf]);
        return $this->toback();
    }

    /**
     * 删除
     * @param int $id
     */
    public function del($id)
    {
        $res = (array) $this->dbConn($this->dbDrug)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有药品');
        }
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbDrug)->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /***
     * 分类列表
     * @param array $pages【start、limit】
     */
    public function cateIndex($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbDrugCate)->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback();
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'uptime' => $v['uptime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 添加
     * @param string $title
     */
    public function cateAdd($title)
    {
        $res = (array) $this->dbConn($this->dbDrugCate)
            ->where('del', 0)
            ->where('title', $title)
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '已存在');
        }
        $pas['title'] = $title;
        $pas['uptime'] = $pas['addtime'] = toDate();
        $lastid = $this->dbConn($this->dbDrugCate)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 修改
     * @param int $id
     * @param string $title
     */
    public function cateModify($id, $title)
    {
        $res = (array) $this->dbConn($this->dbDrugCate)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $pas['title'] = $title;
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbDrugCate)->where('id', $id)->update($pas);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 删除
     * @param int $id
     */
    public function cateDel($id)
    {
        $res = (array) $this->dbConn($this->dbDrugCate)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbDrugCate)->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /***
     * 标签列表
     * @param array $pages【start、limit】
     */
    public function tagIndex($pages, $drug_id)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbDrugTag)
            ->where('del', 0)
            ->where('drug_id', $drug_id);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback();
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 添加
     * @param string $title
     */
    public function tagAdd($drug_id, $title)
    {
        $res = (array) $this->dbConn($this->dbDrugTag)
            ->where('del', 0)
            ->where('drug_id', $drug_id)
            ->where('title', $title)
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '已存在');
        }
        $pas['drug_id'] = $drug_id;
        $pas['title'] = $title;
        $pas['addtime'] = toDate();
        $lastid = $this->dbConn($this->dbDrugTag)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 删除
     * @param int $id
     */
    public function tagDel($id)
    {
        $res = (array) $this->dbConn($this->dbDrugTag)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbDrugTag)->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }
}